package 数组;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;

/**
 * Author: Zhang Dongwei
 * Date: 2022/4/1 12:35
 * 给你一个整数数组 nums，请你将该数组升序排列。
 */
public class 排序数组_912 {
    public static void main(String[] args) {
        int[] nums = new int[] {5,2,3,1};
        System.out.println(Arrays.toString(nums));
        int[] res = sortArray(nums);
        System.out.println(Arrays.toString(res));

    }

    // 官方快速排序
    public static int[] sortArray(int[] nums) {
        randomizedQuicksort(nums, 0, nums.length - 1);
        return nums;
    }

    public static void randomizedQuicksort(int[] nums, int l, int r) {
        if (l < r) {
            int pos = randomizedPartition(nums, l, r);
            randomizedQuicksort(nums, l, pos - 1);
            randomizedQuicksort(nums, pos + 1, r);
        }
    }

    public static int randomizedPartition(int[] nums, int l, int r) {
        int i = new Random().nextInt(r - l + 1) + l; // 随机选一个作为我们的主元
        swap(nums, r, i);
        return partition(nums, l, r);
    }

    public static int partition(int[] nums, int l, int r) {
        int pivot = nums[r];
        int i = l - 1;
        for (int j = l; j <= r - 1; ++j) {
            if (nums[j] <= pivot) {
                i = i + 1;
                swap(nums, i, j);
            }
        }
        swap(nums, i + 1, r);
        return i + 1;
    }

    private static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

}
